package com.thinkgem.jeesite.modules.cloudfactory.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.utils.UserUtil;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUser;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryUserFactory;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactroyOrder;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryUserFactoryService;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactroyOrderService;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 
* @ClassName: LoginCenterController
* @Description: 用户登陆注册、个人中心
* @author Stone
* @date 2018年9月2日
*
 */
@RequiresPermissions("user")
@Controller
@RequestMapping("${adminPath}/userCenter")
public class UserCenterController {
	@Autowired
	private CloudfactoryUserFactoryService cloudfactoryUserFactoryService;
	@Autowired
	private CloudfactroyOrderService cloudfactroyOrderService;
	/**
	 * 
	* @Title: goLogin
	* @Description: 跳转到个人中心页面
	* @param @param model
	* @param @param request
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goUserCenter", method=RequestMethod.GET)
	public String goLogin(Model model,HttpServletRequest request){
		model.addAttribute("isLogin",1);
		List<CloudfactroyOrder>  list = cloudfactroyOrderService.getOrdersByUser(request);
		if(list.size()>5) {
			List<CloudfactroyOrder> userCenter = new ArrayList<CloudfactroyOrder>();
		for(int i=0;i<list.size();i++) {
			userCenter.add(list.get(i));
			
		}
		model.addAttribute("cloudfactoryOrder",userCenter);
		} else {
			model.addAttribute("cloudfactoryOrder",list);
		}
		
		

		return "cloudfactory/usercenter";
	}
	
	
	/**
	 * 
	* @Title: goMyFactory
	* @Description: 跳转到我的工厂界面
	* @param @param model
	* @param @param request
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goMyFactory", method=RequestMethod.GET)
	public String goMyFactory(Model model,HttpServletRequest request){
		model.addAttribute("isLogin",1);
		CloudfactoryUser user = UserUtil.getSessionUser(request);
		CloudfactoryUserFactory cloudfactoryUserFactory = new CloudfactoryUserFactory();
		User us = new User();
		us.setId(user.getId());
		cloudfactoryUserFactory.setUser(us);
		List<CloudfactoryUserFactory> factory = cloudfactoryUserFactoryService.findList(cloudfactoryUserFactory);
		model.addAttribute("factory",factory);
		return "cloudfactory/myfactory";
	}
	/**
	 * 
	* @Title: goAndMyFactory
	* @Description: 跳转到添加工厂
	* @param @param model
	* @param @param request
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value="/goAndMyFactory", method=RequestMethod.GET)
	public String goAndMyFactory(Model model,HttpServletRequest request){
		model.addAttribute("isLogin",1);
		
		return "cloudfactory/addmyfactory";
	}
	/**
	 * 
	* @Title: saveFactory
	* @Description: 保存用户工厂
	* @param @param request
	* @param @param response
	* @param @param model
	* @param @return    参数
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "/saveFactory", method=RequestMethod.POST )
	@ResponseBody
	public String saveFactory(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
			String success = cloudfactoryUserFactoryService.saveFactory(request,model);

		return success;
	}
}
